package com.neusoft.realsight.apm.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.neusoft.realsight.apm.util.GlobalVar;

/**
 * Created by Administrator on 2016/6/10.
 */
public class ConnectorFactory {
    static {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("不能找到org.postgresql.Driver");
        }
    }
    public static Connection getMysqlConnection() throws SQLException {
        Connection conn = null;
        Properties argsPro = new Properties();
        argsPro.setProperty("user",GlobalVar.MYSQL_USER);
        argsPro.setProperty("password",GlobalVar.MYSQL_PASSWORD);
        //需要设置以下两个参数才能获取表的元信息
        argsPro.setProperty("remarks","true");
        argsPro.setProperty("useInformationSchema", "true");

        argsPro.setProperty("characterEncoding", "UTF-8");
        conn = DriverManager.getConnection(GlobalVar.MYSQL_URL,GlobalVar.MYSQL_USER,GlobalVar.MYSQL_PASSWORD);

        //conn = DriverManager.getConnection(GlobalVar.MYSQL_URL,GlobalVar.MYSQL_USER,GlobalVar.MYSQL_PASSWORD);
        return conn;
    }
    
    public static ResultSet query(Connection conn, String sql) {
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        return rs;
    }

}
